package testJdk.io.object_stream;

import java.io.*;
import java.util.Date;

/**
 * @ClassName : com.io.object_stream.TestObjectStream
 * @Description : 类描述
 * Created by cyd on 2020-12-10 23:19:46
 */
public class TestObjectStream {
    public static void main(String[] args) {
        try {
            TestObjectStream.write();
            TestObjectStream.read();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    //抛出异常，由方法调用者处理
    public static void write() throws IOException {
        //显示层次关系
        FileOutputStream fos = new FileOutputStream("D:\\readme.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        //写入数据
        oos.writeInt(1);
        oos.writeDouble(3.14);
        oos.writeBoolean(true);
        oos.writeUTF("cyd陈俞大");
        //写入对象
        oos.writeObject(new Date());
        //关闭流
        oos.close();
    }

    public static void read() throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream("D:\\readme.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        ObjectInputStream ois = new ObjectInputStream(bis);

        System.out.println(ois.readInt());
        System.out.println(ois.readDouble());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readUTF());
        //读取对象
        Date date = (Date) ois.readObject();
        System.out.println(date);

        ois.close();
    }
}
